package com.yyge.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyge.common.constans.RedisConstans;
import com.yyge.entity.Contamer;
import com.yyge.entity.SysDictData;
import com.yyge.service.ContamerService;
import com.yyge.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 仪表盘工具类
 */
@Component
public class DashboardUtil {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ContamerService contamerService;

    /**
     * Sprinq Bean生命周期回调函数
     */
    @PostConstruct
    public  void lifeCirleInit(){
        this.removeAllDateRePort();
        this.currentDateRePortInit();
    }

    /**
     * 加载sys_dict_data所有的数据到redis缓存中
     * @return boolean
     */

    public void currentDateRePortInit(){
        //获取到当前的月份 一年月份最多也就12个月
        int month=Integer.parseInt(DateUtil.format(new Date(),"MM"));

        //获取到当前的年份
        String year=DateUtil.format(new Date(),"yyyy");

        /*循环获取每个月注册的人数*/
        for (int i = 1; i <= month ; i++) {
            String countStr=i<10?("0"+i): String.valueOf(month);
            String key=year+countStr;
            //contamer:202307
            String redisKey= RedisConstans.getRedisKey(RedisConstans.CONTAMER_HASH_KEY,key);

            //前一天的数据统计

            int m=i-month;
            DateTime dateTime = DateUtil.offsetMonth(new Date(), m);
            Date yesterday= DateUtil.offsetDay(dateTime,-1);
            System.out.println("当前仪表盘缓存Key:"+key);
            System.out.println("当前真正的时间："+new Date()+"\tDateUtil计算出的当前时间："+dateTime.toDateStr()+"\t当前时间减一天"+yesterday);
            //前一天开始
            Date begin=DateUtil.beginOfDay(yesterday);
            //Date begin=DateUtil.beginOfDay(dateTime);
            //前一天结束
            Date end=DateUtil.endOfDay(dateTime);

            LambdaQueryWrapper<Contamer>  queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.between(Contamer::getCreateTime,begin,end);
            System.out.println("查询区间--begin:"+begin.toString()+"\tend:"+end);
            long count = this.contamerService.count(queryWrapper);
            System.out.println("查询结果："+count);

            //写入到redis中 day是在一个月中的多少天
            String day = String.valueOf(DateUtil.dayOfMonth(dateTime));
            this.redisTemplate.opsForHash().put(redisKey,day,Integer.parseInt(count+""));
        }
    }

    /**
     * 移出redis中所有的sys_dict_data缓存
     */
    public  void removeAllDateRePort(){
        String redisKey=RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY,"*");
        Set keys = this.redisTemplate.keys(redisKey);
        this.redisTemplate.delete(keys);
    }


    /**
     * 刷新redis的缓存数据
     * 刷新缓存中数据报表的信息
     */
    public void refreshCacheDateRePort(){
        this.removeAllDateRePort();
        this.currentDateRePortInit();
    }


}

